using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class MainMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.main_Menu, new Point(0, -107));
    private static bool inited;

    public MainMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            Build();
            inited = true;
        }
    }

    private static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        var playerItem = new UIMenuItem(TextSetting.main_MenuItem_PlayerMenu);
        var gfItem = new UIMenuItem(TextSetting.main_MenuItem_GirlfriendMenu);
        var interactItem = new UIMenuItem(TextSetting.main_MenuItem_Interact, TextSetting.main_MenuItem_Interact_dct);

        menu.AddItem(playerItem);
        menu.AddItem(gfItem);
        menu.AddItem(interactItem);

        menu.BindMenuToItem(PlayerMenu.menu, playerItem);
        menu.BindMenuToItem(GirlfriendMenu.menu, gfItem);
        // 动态启用/禁用互动项：步行时可用；在车内仅当 P1 为驾驶位可用；女友不存在或距离>40f则禁用
        try
        {
            bool inVeh = Main.P1.Character != null && Main.P1.Character.IsInVehicle();
            bool isDriver = inVeh && Main.IsPedDriverOfCurrentVehicle(Main.P1.Character);
            bool gfNear = Main.IsGirlfriendInNear(40f);
            interactItem.Enabled = (((!inVeh) || isDriver) && gfNear);
        }
        catch { interactItem.Enabled = true; }
        menu.OnItemSelect += (sender, item, index) =>
        {
            switch (index)
            {
                case 0:
                    PlayerMenu.Open();
                    menu.Visible = false;
                    Main.OnMainMenuHidden();
                    break;
                case 1:
                    GirlfriendMenu.Open();
                    menu.Visible = false;
                    Main.OnMainMenuHidden();
                    break;
                case 2:
                    if (Main.P1.Character != null && !Main.P1.Character.IsInVehicle())
                    {
                        Main.m_Status = Main.MaleStatus.OnFoot;
                        OnFootMainMenu.Open();
                    }
                    if (Main.P1.Character != null && Main.P1.Character.IsInVehicle() && Main.IsPedDriverOfCurrentVehicle(Main.P1.Character))
                    {
                        Main.m_Status = Main.MaleStatus.Vehicle;
                        VehicleMainMenu.Open();
                    }
                    menu.Visible = false;
                    Main.OnMainMenuHidden();
                    break;
            }
        };

        menu.RefreshIndex();
    }
}


